package com.jams.goodox.activity.mine.ox;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.hangzhou.jin.customview.SpaceItemDecoration;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.Utils;
import com.jams.goodox.widget.SimpleDividerItemDecoration;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/27.
 */

public class OxRecordActivity extends BaseActivity{

    @Bind(R.id.recycle_view)
    RecyclerView recyclerView;
    List<OxRecordBean> oxInbeanList;


    private int pageNum = 1;
    private int pageSize = 20;
    @Override
    protected int getLayoutId() {
        return R.layout.card_cz;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this,"收入记录");
        oxInbeanList=new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new RecycleCommonAdapter<OxRecordBean>(this,oxInbeanList,R.layout.order_record_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final OxRecordBean item, int position) {
                helper.setText(R.id.tx_money,item.getMoneyType()+item.getCoinCount());
                TextView tx=helper.getView(R.id.tx_money);
                tx.setTextColor(ContextCompat.getColor(mContext,item.getOperationType()==1?R.color.order_main_theme:R.color.green));
                helper.setText(R.id.tx_type,item.getType());
                helper.setText(R.id.tx_time, Utils.formatDateTime(item.getCreateTime()));
            }
        });
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
        getDt();
    }

    private void getDt(){
        A a=new A();
        a.memberId=SePref.getId();
        a.pageNum=pageNum;
        a.pageSize=pageSize;
        enqueue(createCallService(MineService.class).getOxOrder(ProtocolManagers.getRequestMap(a)), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                if (TextUtils.isEmpty(result.getResponseData())) return;
                try{
                    JsonArray jsonArray = new JsonParser().parse(result.getResponseData()).getAsJsonArray();
                    oxInbeanList.clear();
                    for(JsonElement jsonElement:jsonArray){
                        OxRecordBean message = new Gson().fromJson(jsonElement, OxRecordBean.class);
                        oxInbeanList.add(message);
                    }
                    recyclerView.getAdapter().notifyDataSetChanged();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    static class A{
        int memberId;
        int pageNum;
        int pageSize;
    }
}
